package com.example.vue_shop.components.shoptype.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vue_shop.components.shoptype.entity.ShopType;
import com.example.vue_shop.components.shoptype.mapper.ShopTypeMapper;
import com.example.vue_shop.components.shoptype.service.ShopTypeService;
import com.example.vue_shop.utils.TreeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lipengwei
 */
@Service
public class ShopTypeServiceImpl implements ShopTypeService {

    @Resource(name = "shopTypeMapper")
    private ShopTypeMapper shopTypeMapper;

    @Override
    public Map<String, Object> getAllShopTypeListPage(String type,
                                                      int pageNum, int pageSize) {
        Page<Map<String, Object>> queryPage = new Page<>();
        queryPage.setCurrent(pageNum);
        queryPage.setSize(pageSize);

        QueryWrapper<ShopType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_type_grade", "1");
        Page<Map<String, Object>> mapPage = shopTypeMapper.selectMapsPage(queryPage, queryWrapper);
        List<Map<String, Object>> shopTypeMapList = mapPage.getRecords();

        // 查询所有分类列表
        queryWrapper.clear();
        queryWrapper.le("shop_type_grade", type);
        List<Map<String, Object>> shopTypeAllMapList = shopTypeMapper
                .selectMaps(queryWrapper);
        List<Map<String, Object>> pageTree = TreeUtils.
                getPageTree(shopTypeMapList, shopTypeAllMapList, "shopTypeId"
                        , "shopTypeParentId");

        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put("total", mapPage.getTotal());
        resultMap.put("shopTypeMapListTree", pageTree);
        return resultMap;
    }

    @Override
    public Map<String, Object> getAllShopTypeList(String type) {
        QueryWrapper<ShopType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_type_grade", "1");
        List<Map<String, Object>> oneGradeMap =
                shopTypeMapper.selectMaps(queryWrapper);

        queryWrapper.clear();
        queryWrapper.le("shop_type_grade", type);
        List<Map<String, Object>> maps = shopTypeMapper.selectMaps(queryWrapper);
        List<Map<String, Object>> pageTree = TreeUtils.getPageTree(oneGradeMap, maps, "shopTypeId", "shopTypeParentId");
        Map<String, Object> resultMap = new HashMap<>(1);
        resultMap.put("shopTypeMapListTree", pageTree);
        return resultMap;
    }

    @Override
    public void addShopType(ShopType shopType) {
        shopTypeMapper.insert(shopType);
    }

    @Override
    public void delShopType(int id) {
        shopTypeMapper.deleteById(id);
    }

    @Override
    public void updateShopType(ShopType shopType) {
        shopTypeMapper.updateById(shopType);
    }
}
